// 发送 Ajax 请求之前，将 csrf 令牌设置到请求头中
// var token = $("meta[name='_csrf']").attr("content");
// var header = $("meta[name='_csrf_header']").attr("content");
// $(document).ajaxSend(function (e, xhr, options) {
//     xhr.setRequestHeader(header, token);
// });

$(function () {
    $("#topBtn").click(setTop);
    $("#wonderfulBtn").click(setWonderful);
    $("#deleteBtn").click(setDelete);
});

function like(btn, entityType, entityId, entityUserId, postId) {
    $.post(
        CONTEXT_PATH + "/like",
        {
            "entityType": entityType,
            "entityId": entityId,
            "entityUserId": entityUserId,
            "postId": postId
        },
        function (data) {
            console.log(data);
            if (data.code == 0) {
                $(btn).children("i").text(data.data.likeCount);
                $(btn).children("b").text(data.data.likeStatus == 1 ? '已赞' : '赞');
            } else if (data == "login") window.location.href = CONTEXT_PATH + "/login";
            else if (data == "denied") window.location.href = CONTEXT_PATH + "/denied";
            else alert(data.msg);
        }
    );
}

// 置顶
function setTop() {
    $.post(
        CONTEXT_PATH + "/discuss/top",
        {"id": $("#postId").val()},
        function (data) {
            if (data.code == 0) $("#topBtn").attr("disabled", true);
            else if (data == "login") window.location.href = CONTEXT_PATH + "/login";
            else if (data == "denied") window.location.href = CONTEXT_PATH + "/denied";
            else alert(data.msg);
        }
    );
}

// 加精
function setWonderful() {
    $.post(
        CONTEXT_PATH + "/discuss/wonderful",
        {"id": $("#postId").val()},
        function (data) {
            if (data.code == 0) $("#wonderfulBtn").attr("disabled", true);
            else if (data == "login") window.location.href = CONTEXT_PATH + "/login";
            else if (data == "denied") window.location.href = CONTEXT_PATH + "/denied";
            else alert(data.msg);
        }
    );
}

// 删除
function setDelete() {
    $.post(
        CONTEXT_PATH + "/discuss/delete",
        {"id": $("#postId").val()},
        function (data) {
            if (data.code == 0) location.href = CONTEXT_PATH + "/index";
            else if (data == "login") window.location.href = CONTEXT_PATH + "/login";
            else if (data == "denied") window.location.href = CONTEXT_PATH + "/denied";
            else alert(data.msg);
        }
    );
}